<?php
namespace Badmins\Controller;
class OrderController extends CkadminController {
    public function index(){
    	$db1 = M('order');
    	$map['is_produce'] = 0;
    	$map['user_id'] = $this->uid;
    	$map['order_status'] = 0;
    	$map['pay_type'] = 0;
    	$db1->where($map)->delete();
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$where = array();
    	$status = I('order_status');
    	if($status!=""){
    		$where['o.order_status'] = $status;
    	}
    	$pay_type = I('pay_type');
    	if($pay_type!=""){
    		$where['o.pay_type'] = $pay_type;
    	}
    	$order_no = I('order_no');
    	if(!empty($order_no)){
    		$where['o.order_no'] = array('like',"%{$order_no}%");
    	}
    	$name = I('name');
    	if(!empty($name)){
    		$map['u.name'] = array('like',"%{$name}%");
    		$map['u.nickname'] = array('like',"%{$name}%");
    		$map['u.mobile'] = array('like',"%{$name}%");
    		$map['_logic'] = 'or';
    		$where['_complex'] = $map;
    	}
    	$start_time = I('start_time')?I('start_time'):'1990-01-01';
    	$end_time = I('end_time')?I('end_time'):'2550-01-01';
    	$where['o.create_time'] = array('between',array($start_time,$end_time));
    	$where['o.mobile'] = array('neq','');
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$join = "left join __USER__ as u on u.id = o.user_id";
    	$db = M('order as o');
    	$count = $db->join($join)->where($where)->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('order as o',$join,$where,"o.*,u.name,u.mobile as user_mobile","o.id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
        $this->display();
    }
    /**
     * 订单详情
     */
    public function msg(){
    	$id = I('id');
    	$order = M('order')->where("id='{$id}'")->find();
    	if(empty($order)){
    		showmsg(300,'此订单不存在');exit();
    	}
    	$user = M('user')->where("id='{$order['user_id']}'")->field("name,mobile")->find();
    	$ordergoodslist = M('order_goods as og')->join("left join  __GOODS__  as g on og.goods_id=g.id left join __PRODUCTS__ as p on p.id=og.product_id")->where("og.order_id='{$order['id']}'")->field("og.goods_id,og.goods_price,og.goods_nums,g.img,g.name,p.spec_array,og.product_id,p.store_nums,SUBSTRING_INDEX(p.photo_list,',',1) as product_img")->select();
    	$orderlog = M('order_log')->where("order_id='{$order['id']}'")->select();
    	$this->assign('orderlog',$orderlog);
    	$this->assign('order',$order);
    	$this->assign('ordergoods',$ordergoodslist);
    	$this->assign('user',$user);
    	
    	//发货记录
    	$freightlist  =  M('delivery_doc as dd')->join("left join __EXPRESS_LIST__ as el on dd.freight_id = el.id left join __ADMIN__ as a on a.id = dd.admin_id")->where("dd.order_id='{$id}'")->field("dd.*,el.name,a.username")->order("id asc")->select();
    	$this->assign('freightlist',$freightlist);
    	$this->display();
    }
    /**
     * 修改订单
     */
    public function update_order(){
    	$db = M('order');
    	$admin = $this->admin_infor();
    	$order_no = I('order_no');
    	$order_status = I('order_status');
    	$notee = I('note');
    	if(empty($notee)){
    		showmsg(300,'请输入操作备注');exit();
    	}
    	$id = I('id');
    	if($order_status == 1){
    		$re = $db->where("id='{$id}' and order_status='0' and pay_type !='0'")->find();
    		if(empty($re)){
    			showmsg(300,'此订单不能付款');exit();
    		}
    		$action = "付款";
    		$user = $admin['username'];
    		$note = "订单号【".$order_no."】由【商城管理员】代付款";
    		//$data['order_status'] = $order_status;
    		\Org\Oa\order::update_order_status($re['order_no'],0,1,$notee);
    		\Org\Oa\order::order_log($id, $user, $action, $note);
    	}elseif ($order_status == 0){
    		$re = $db->where("id='{$id}' and order_status='1' and pay_type !='0'")->find();
    		if(empty($re)){
    			showmsg(300,'此订单不能设为未付款状态');exit();
    		}
    		$action = "设为未付款";
    		$user = $admin['username'];
    		$note = "订单号【".$order_no."】由【商城管理员】由付款变成未付款";
    		\Org\Oa\order::update_order_status($re['order_no'],1,0,$notee);
    		\Org\Oa\order::order_log($id, $user, $action, $note);
    	}elseif ($order_status == 4){
    		$re = $db->where("id='{$id}' and order_status='3'")->find();
    		if(empty($re)){
    			showmsg(300,'此订单不能收货');exit();
    		}
    		$action = "收货";
    		$user = $admin['username'];
    		$note = "订单号【".$order_no."】由【商城管理员】代为收货，此订单已完成";
    		\Org\Oa\order::update_order_status($re['order_no'],3,4,$notee);
    		\Org\Oa\order::order_log($id, $user, $action, $note);
    	}
    	showmsg(200,'操作成功');
    }
    /**
     * 发货单
     */
    public function send(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$where = array();
    	$pay_type = I('pay_type');
    	if($pay_type!=""){
    		$where['o.pay_type'] = $pay_type;
    	}
    	$order_no = I('order_no');
    	if(!empty($order_no)){
    		$where['o.order_no'] = array('like',"%{$order_no}%");
    	}
    	$name = I('name');
    	if(!empty($name)){
    		$map['u.name'] = array('like',"%{$name}%");
    		$map['u.nickname'] = array('like',"%{$name}%");
    		$map['u.mobile'] = array('like',"%{$name}%");
    		$map['_logic'] = 'or';
    		$where['_complex'] = $map;
    	}
    	$start_time = I('start_time')?I('start_time'):'1990-01-01';
    	$end_time = I('end_time')?I('end_time'):'2550-01-01';
    	$where['o.create_time'] = array('between',array($start_time,$end_time));
    	$where['o.order_status'] = array('in','1,2,3');
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$join = "left join __USER__ as u on u.id = o.user_id";
    	$db = M('order as o');
    	$count = $db->join($join)->where($where)->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('order as o',$join,$where,"o.*,u.name,u.mobile as user_mobile","o.id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 操作发货
     */
    public function msgtwo(){
    	$id = I('id/d');
    	if($id){
    		$data['id'] = $id;
    		$data['order_status'] = array('in','1,2,3');
    		$order = M('order')->where($data)->find();
    		if(empty($order)){
    			showmsg(300,'此订单不存在');exit();
    		}
    		$this->assign('va',$order);
    		$ordergoodslist = M('order_goods as og')->join("left join  __GOODS__  as g on og.goods_id=g.id left join __PRODUCTS__ as p on p.id=og.product_id")->where("og.order_id='{$order['id']}'")->field("og.id,og.is_send,og.goods_id,og.goods_price,og.goods_nums,g.img,g.name,p.spec_array,og.product_id,p.store_nums,SUBSTRING_INDEX(p.photo_list,',',1) as product_img")->select();
    		$this->assign('ordergoods',$ordergoodslist);
    		$freight = M('ExpressList')->where("status='0'")->order("id desc")->select();
    		$this->assign('freight',$freight);
    		//发货记录
    		$freightlist  =  M('delivery_doc as dd')->join("left join __EXPRESS_LIST__ as el on dd.freight_id = el.id left join __ADMIN__ as a on a.id = dd.admin_id")->where("dd.order_id='{$id}'")->field("dd.*,el.name,a.username")->order("id asc")->select();
    		$this->assign('freightlist',$freightlist);
    		$this->display();
    	}else{
    		showmsg(300,'此订单不存在');
    	}
    }
    /**
     * 删除订单
     */
    public function del_order(){
    	$id = I('id/d');
    	$list = M('order')->where("id='{$id}' and order_status='0'")->find();
    	if(!empty($list)){
    		M('order')->where("id='{$id}'")->delete();
    		$log = "删除了一条订单,订单号为：".$list['order_no'];
    		$this->add_log($log);
    		showmsg(200,'删除成功');
    	}else{
    		showmsg(300,'删除失败');
    	}
    }
    /**
     * 发货
     */
    public function freight_order(){
    	$id = I('id/d');
    	$goods_id = I('goods_id');
    	if(empty($goods_id[0])){
    		showmsg(300,'请选择你要发货的商品');exit();
    	}
    	$delivery_code = I('delivery_code');
    	$freight_id = I('freight_id');
    	if(empty($freight_id) || empty($delivery_code)){
    		showmsg(300,'请输入发货信息');exit();
    	}
    	$db = M('order_goods');
    	$flag = 1;
    	foreach ($goods_id as $i=>$va){
    		$re[$i] = $db->where("id='{$va}' and order_id='{$id}'")->find(); 
    		if(empty($re[$i])){
    			$flag *= 0;
    		}else{
    			$flag *= 1;
    		}
    	}
    	if($flag == 1){
    		$admin = $this->admin_infor();
    		$orderDb = M('order');
    		$order = $orderDb->where("id='{$id}'")->find();
    		$ce = array();
    		$ce['id'] = array('in',$goods_id);
    		$db->where($ce)->setField('is_send',1);
    		//添加到发货日志
    		$map['order_id'] = $id;
    		$map['user_id'] = $order['user_id'];
    		$map['admin_id'] = $admin['id'];
    		$map['time'] = date("Y-m-d H:i:s");
    		$map['delivery_code'] = $delivery_code;
    		$map['note'] = I('note');
    		$map['freight_id']  = $freight_id;
    		M('delivery_doc')->add($map);
    		//改变订单状态
    		$count = $db->where("order_id='{$id}' and is_send='0'")->find();
    		if(empty($count)){
    			$data['order_status'] = 3;
    		}else{
    			$data['order_status'] = 2;
    		}
    		\Org\Oa\order::update_order_status($order['order_no'],0, $data['order_status']);
    		//添加到订单日志
    		$action = "发货";
    		$user = $admin['username'];
    		$note = "订单号【".$order['order_no']."】由【商城管理员】发货";
    		\Org\Oa\order::order_log($id, $user, $action, $note);
    		showmsg(200,'发货成功');exit();
    	}else{
    		showmsg(300,'发货失败');exit();
    	}
    }
    /**
     * 退款单
     */
    public function returned(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$where = array();
    	$pay_type = I('pay_status');
    	if($pay_type!=""){
    		$where['rd.pay_status'] = $pay_type;
    	}
    	$order_no = I('order_no');
    	if(!empty($order_no)){
    		$where['o.order_no'] = array('like',"%{$order_no}%");
    	}
    	/* $name = I('name');
    	if(!empty($name)){
    		$map['u.name'] = array('like',"%{$name}%");
    		$map['u.nickname'] = array('like',"%{$name}%");
    		$map['u.mobile'] = array('like',"%{$name}%");
    		$map['_logic'] = 'or';
    		$where['_complex'] = $map;
    	} */
    	$start_time = I('start_time')?I('start_time'):'1990-01-01';
    	$end_time = I('end_time')?I('end_time'):'2550-01-01';
    	$where['rd.time'] = array('between',array($start_time,$end_time));
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$join = "left join __ORDER__ as o on o.id = rd.order_id left join __USER__ as u on u.id = rd.user_id";
    	$db = M('refundment_doc as rd');
    	$count = $db->join($join)->where($where)->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('refundment_doc as rd',$join,$where,"rd.*,o.order_no,u.name,u.mobile","rd.pay_status asc,rd.id desc",$firstrow,$listrow,2);
    	if(!empty($list)){
    		foreach ($list as $i=>$va){
    			if($va['admin_id']){
    				$list[$i]['adminname'] = M('admin')->where("id='{$va['admin_id']}'")->getField('username');
    			}
    		}
    	}
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    /**
     * 退款详情
     */
    public function msgthree(){
    	$id = I('id/d');
    	if($id){
    		$where['rd.id'] = $id;
    		$join = "left join __ORDER__ as o on o.id = rd.order_id left join __USER__ as u on u.id = rd.user_id";
    		$list = lists('refundment_doc as rd',$join,$where,"rd.*,o.order_no,u.name,u.mobile","rd.id desc",0,0,1);
    		$this->assign('va',$list);
    		$ordergoodslist = M('order_goods as og')->join("left join  __GOODS__  as g on og.goods_id=g.id left join __PRODUCTS__ as p on p.id=og.product_id")->where("og.order_id='{$list['order_id']}'")->field("og.goods_id,og.goods_price,og.goods_nums,g.img,g.name,p.spec_array,og.product_id,p.store_nums,SUBSTRING_INDEX(p.photo_list,',',1) as product_img")->select();
    		$this->assign('ordergoods',$ordergoodslist);
    	}
    	$this->display();
    }
    /**
     * 审核退款申请
     */
    public function save_refund(){
    	$dispose_idea = I('dispose_idea');
    	if(empty($dispose_idea)){
    		showmsg(300,'请输入处理意见');exit();
    	}
    	$id = I('id/d');
    	$db = M('refundment_doc');
    	$list = $db->where("id='{$id}' and pay_status='0'")->find();
    	if(empty($list)){
    		showmsg(300,'此申请已审核啦');exit();
    	}
    	$pay_status = I('pay_status');
    	$admin = $this->admin_infor();
    	$order_no = M('order')->where("id='{$list['id']}'")->getField('order_no');
    	if($pay_status == 2){
    		//退款
    		$order_status = 6;
    		$action = "退款审核通过";
    		$user = $admin['username'];
    		$note = "订单号【".$order_no."】由【商城管理员】审核退款通过";
    	}else{
    		$order_status = 1;
    		$action = "退款审核失败";
    		$user = $admin['username'];
    		$note = "订单号【".$order_no."】由【商城管理员】审核退款失败，订单状态变成待发货";
    	}
    	$data['pay_status'] = $pay_status;
    	$data['admin_id'] = session('shopping_admin_id');
    	$data['dispose_time'] = date("Y-m-d H:i:s");
    	$data['dispose_idea'] = $dispose_idea;
    	$data['id'] = $id;
    	$res = $db->save($data);
    	if($res!=false){
    		M('order')->where("id='{$list['order_id']}'")->setField('order_status',$order_status);
    		\Org\Oa\order::order_log($list['order_id'], $user, $action, $note);
    		showmsg(200,'审核通过');
    	}else{
    		showmsg(300,'审核失败');
    	}
    }
    /**
     * 订单日志
     */
    public function log(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$where = array();
    	$order_no = I('order_no');
    	if(!empty($order_no)){
    		$where['o.order_no'] = array('like',"%{$order_no}%");
    	}
    	$name = I('name');
    	if(!empty($name)){
    		$where['a.username'] = array('like',"%{$name}%");
    	}
    	$start_time = I('start_time')?I('start_time'):'1990-01-01';
    	$end_time = I('end_time')?I('end_time'):'2550-01-01';
    	$where['ol.addtime'] = array('between',array($start_time,$end_time));
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$join = "left join __ORDER__ as o on o.id = ol.order_id";
    	$db = M('order_log as ol');
    	$count = $db->join($join)->where($where)->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('order_log as ol',$join,$where,"ol.*,o.order_no","ol.id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
}